﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Routing;

namespace System.Web.Mvc.Html
{
    public static class HtmlHelperExtensions
    {
        #region 变量
        private static string missingMessage = "必填";
        #endregion 变量


        #region 文本框
        /// <summary>
        /// 录入框
        /// </summary>
        /// <param name="htmlHelper">被扩展的HtmlHelper实例</param>
        /// <param name="name">录入框的name属性</param>
        /// <param name="value">录入框的value属性</param>
        /// <param name="validType">验证类型</param>
        /// <param name="IsRequired">是否必填</param>
        /// <param name="htmlAttributes">html属性集合</param>
        /// <param name="missingMessage">当文本框是空时出现的提示文字</param>
        /// <param name="invalidMessage">当文本框的内容无效时出现的提示文字</param>
        /// <returns>录入框的html代码</returns>
        public static MvcHtmlString IssTextBox(this HtmlHelper htmlHelper, string name, string value = null, ValidType? validType = null, bool IsRequired = false, int maxLength = -1, string invalidMessage = "格式不正确", RouteValueDictionary htmlAttributes = null)
        {
            if (htmlAttributes == null)
                htmlAttributes = new RouteValueDictionary();
            htmlAttributes.AddCssClass("txt03");

            if (IsRequired)
            {
                htmlAttributes.Add("required", "true");
                htmlAttributes.Add("missingMessage", missingMessage);

            }
            if (validType != null)
            {
                switch (validType)
                {
                    case ValidType.Int:
                        htmlAttributes.AddCssClass("easyui-numberbox");

                        break;

                    case ValidType.String:
                        htmlAttributes.AddCssClass("easyui-validatebox");
                        if (maxLength != -1)
                            htmlAttributes.Add("validType", string.Format(@"length[0,{0}]", maxLength));
                        break;

                }
            }

            if (!string.IsNullOrEmpty(invalidMessage))
                htmlAttributes.Add("invalidMessage", invalidMessage);

            return InputExtensions.TextBox(htmlHelper, name, value, htmlAttributes);
        }


        public static MvcHtmlString IssTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, ValidType? validType = null, bool IsRequired = false, int maxLength = -1, string invalidMessage = "格式不正确", RouteValueDictionary htmlAttributes = null)
        {
            if (htmlAttributes == null)
                htmlAttributes = new RouteValueDictionary();
            htmlAttributes.AddCssClass("txt03");

            if (IsRequired)
            {
                htmlAttributes.Add("required", "true");
                htmlAttributes.Add("missingMessage", missingMessage);

            }
            if (validType != null)
            {
                switch (validType)
                {
                    case ValidType.Int:
                        htmlAttributes.AddCssClass("easyui-numberbox");

                        break;

                    case ValidType.String:
                        htmlAttributes.AddCssClass("easyui-validatebox");
                        if (maxLength != -1)
                            htmlAttributes.Add("validType", string.Format(@"length[0,{0}]", maxLength));
                        break;

                }
            }

            if (!string.IsNullOrEmpty(invalidMessage))
                htmlAttributes.Add("invalidMessage", invalidMessage);

            return InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes);
        }
        #endregion 文本框

        #region 私有扩展方法

        /// <summary>
        /// 为RouteValueDictionary扩展添加class方法
        /// </summary>
        /// <param name="htmlAttributes">html属性集合</param>
        /// <param name="cssClass">样式名</param>
        /// <returns>RouteValueDictionary</returns>
        private static RouteValueDictionary AddCssClass(this RouteValueDictionary htmlAttributes, string cssClass)
        {
            if (htmlAttributes == null)
                htmlAttributes = new RouteValueDictionary();
            if (htmlAttributes.Any(n => n.Key.ToLower() == "class"))
                htmlAttributes["class"] += " " + cssClass;
            else
                htmlAttributes["class"] = cssClass;

            return htmlAttributes;
        }

        #endregion 私有扩展方法

        #region 复选框

        public static MvcHtmlString IssCheckBox<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, RouteValueDictionary htmlAttributes = null)
        {
            if (htmlAttributes == null)
                htmlAttributes = new RouteValueDictionary();
            htmlAttributes.AddCssClass("checkbox01");

            return InputExtensions.CheckBoxFor(htmlHelper, expression, htmlAttributes);
        }

        #endregion 复选框
    }

    /// <summary>
    /// 有效类型
    /// </summary>
    public enum ValidType
    {
        Int,
        String,
        EMail,
        Url
    }
}
